package com.dgut.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForUser;
import com.dgut.common.StateOfNotice;
import com.dgut.pojo.Administrator;
import com.dgut.pojo.Buyer;
import com.dgut.pojo.ServiceProvider;
import com.dgut.pojo.User;
import com.dgut.pojo.receive.AdministratorAndBuyerReceive;
import com.dgut.pojo.receive.ReceiveId;
import com.dgut.pojo.receive.ServiceProvideReceive;
import com.dgut.service.*;
import com.dgut.utils.PageRequestInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin
@Api("UserController")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private UserService userService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private OssService ossService;

    /**
     * start-zwb
     */
    @SaCheckLogin
    @ApiOperation(value = "返回登录用户信息",notes = "")
    @PostMapping("/returnUserInformation")
    public Object returnUserInformation() {
       Integer id = Integer.valueOf((String) StpUtil.getLoginId());
       ConcurrentHashMap resultMap = new ConcurrentHashMap();
       resultMap.put("user",userService.returnUserInformation(id));
       return  resultMap;
    }
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    @ApiOperation(value = "用户修改密码",notes = "")
    @PostMapping("/UpdatePassword")
    public Object UpdatePassword(@ApiParam(value = "传用户id和两次密码",required = true)
                                            @RequestParam Integer id,
                                            @RequestParam String password1,
                                            @RequestParam String password2){
        return userService.UpdateUserPassword(id,password1,password2);
    }
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */


    /**
     * 分页获取未读通知
     * @param pageRequestInfo
     * @return
     */
    @SaCheckLogin
    @ApiOperation(value = "分页获取未读通知",notes = "")
    @PostMapping("/getUnreadNotices")
    public Object getUnreadNotices(@ApiParam(value = "pageRequestInfo",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要登录才有能获取通知
         */
        resultMap.put("unreadNoticesPage",noticeService.getNoticePage(Integer.valueOf((String) StpUtil.getLoginId()),StateOfNotice.UNREAD,pageRequestInfo));
        return resultMap;
    }

    /**
     * 分页获取已读通知
     * @param pageRequestInfo
     * @return
     */
    @SaCheckLogin
    @ApiOperation(value = "分页获取已读通知",notes = "")
    @PostMapping("/getReadNotices")
    public Object getReadNotices(@ApiParam(value = "pageRequestInfo",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要登录才有能获取通知
         */
        resultMap.put("readNoticesPage",noticeService.getNoticePage(Integer.valueOf((String) StpUtil.getLoginId()),StateOfNotice.READ,pageRequestInfo));
        return resultMap;
    }


    @SaCheckLogin
    @ApiOperation(value = "设置通知已读",notes = "")
    @PostMapping("/readNotice")
    public Object readNotice(@ApiParam(value = "receiveId",required = true) @RequestBody ReceiveId receiveId){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要登录才有能获取通知
         */
        Integer id = receiveId.getId();
        if(id != null){
            if(noticeService.readNotice(id,Integer.valueOf((String) StpUtil.getLoginId()),StateOfNotice.READ)){
                resultMap.put("result",true);
                resultMap.put("message","设置成功！");
            }else{
                resultMap.put("result",false);
                resultMap.put("message","设置失败！");
            }
        }else{
            resultMap.put("result",false);
            resultMap.put("message","设置失败！");
        }
        return resultMap;
    }

    @SaCheckLogin
    @ApiOperation(value = "修改密码",notes = "")
    @PostMapping("/changePassword")
    public Object changePassword(@ApiParam(value = "password",required = true)@RequestBody User user) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        User newUser = userService.getUser(Integer.valueOf((String) StpUtil.getLoginId()));
        newUser.setPassword(user.getPassword());
        if (user.getPassword() != null && userService.updateUser(newUser)) {
            resultMap.put("result", true);
            resultMap.put("message", "修改成功！");
        } else {
            resultMap.put("result", false);
            resultMap.put("message", "修改失败！");
        }
        return resultMap;
    }


    @ApiOperation(value = "用户重置密码",notes = "")
    @PostMapping("/resetPasswordForBuyer")
    public Object resetPasswordForBuyer(@ApiParam(value = "password",required = true)@RequestBody AdministratorAndBuyerReceive administratorAndBuyerReceive){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(administratorAndBuyerReceive.getUsername() == null ||
        administratorAndBuyerReceive.getPassword() == null ||
        administratorAndBuyerReceive.getName() == null ||
        administratorAndBuyerReceive.getIdCard() == null){
            resultMap.put("result", false);
            resultMap.put("message", "重置失败！");
        }else{
            if(userService.resetPassword(administratorAndBuyerReceive)){
                resultMap.put("result", true);
                resultMap.put("message", "重置成功！");
            }else{
                resultMap.put("result", false);
                resultMap.put("message", "重置失败！");
            }
        }
        return  resultMap;
    }

    @SaCheckLogin
    @ApiOperation(value = "上传文件",notes = "")
    @PostMapping("/uploadFile")
    public Object uploadOssFile(MultipartFile file) {
        //获取上传文件  MultipartFile
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(!file.isEmpty()){
            //返回上传到oss的路径
            String url = ossService.uploadFileAvatar(file);
            if(url != null){
                resultMap.put("url", url);
                resultMap.put("result", true);
                resultMap.put("message", "上传成功！");
            }else{
                resultMap.put("result", false);
                resultMap.put("message", "上传失败！");
            }
        }else{
            resultMap.put("result", false);
            resultMap.put("message", "上传失败！");
        }

        return resultMap;
    }

    /**
     * end-hwf
     */


}
